package com.central.user.controller;

import com.central.model.common.PageResult;
import com.central.user.model.FightInfo;
import com.central.user.model.PlayerBagInfo;
import com.central.user.model.PlayerBaseInfo;
import com.central.user.model.PlayerDetailsInfo;
import com.central.user.service.BsdldService;
import com.central.user.service.PlayerBaseService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class PlayerInfoController {

    @Autowired
    private BsdldService bsdldService;

    @Autowired
    private PlayerBaseService playerBaseService;

    private static final Logger logger = LoggerFactory.getLogger(PlayerInfoController.class);

    @PreAuthorize("hasAuthority('bsdld:get/playerBattleInfo')")
    @ApiOperation(value = "玩家战斗信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"), @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")})
    @GetMapping("/playerBattleInfo")
    public PageResult<FightInfo> findBattleInfo(@RequestParam Map<String, Object> params) {
        logger.info("玩家战斗信息查询控制类");
        return bsdldService.findFightInfoAll(params);
    }


    @PreAuthorize("hasAuthority('bsdld:get/playerBaseInfo')")
    @ApiOperation(value = "玩家基础信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"), @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")})
    @GetMapping("/playerBaseInfo")
    public PageResult<PlayerBaseInfo> findPlayerBaseInfo(@RequestParam Map<String, Object> params) {
        logger.info("玩家基础信息查询控制类");
        /**
         * 若searchKey=playerId,或者没有该参数:那么两边查出的数据条数应当相等
         * 否则,有一边的数据会更少,那么就以少的数据为基准
         */
        PageResult<PlayerBaseInfo> playerByLogin = new PageResult<>();
        PageResult<PlayerBaseInfo> playerByBsdld = new PageResult<>();
        if (params.containsKey("searchKey") && "nickName".equals(params.get("searchKey"))) {
            playerByBsdld = playerBaseService.findPlayerBaseInfoByBsdld(params, true);
            params.remove("searchKey");
            params.remove("searchValue");
            playerByLogin = playerBaseService.findPlayerBaseInfoByLogin(params, false);
        } else {
            playerByLogin = playerBaseService.findPlayerBaseInfoByLogin(params, true);
            playerByBsdld = playerBaseService.findPlayerBaseInfoByBsdld(params, false);
        }
        return returnPlayerInfo(playerByLogin,playerByBsdld);
    }

    private PageResult<PlayerBaseInfo> returnPlayerInfo(PageResult<PlayerBaseInfo> login, PageResult<PlayerBaseInfo> bsdld) {
        if (login.getData().size() <= bsdld.getData().size()){
            for (int i = 0; i < login.getData().size(); i++) {
                login.getData().get(i).setType(login.getData().get(i).getPhone());
                for (int j = 0; j < bsdld.getData().size(); j++) {
                    if (login.getData().get(i).getPlayerId() == bsdld.getData().get(j).getPlayerId()) {
                        login.getData().get(i).setNickName(bsdld.getData().get(j).getNickName());
                        break;
                    }
                }
            }
            return login;
        }else {
            for (int i = 0; i < bsdld.getData().size(); i++) {
                for (int j = 0; j < login.getData().size(); j++) {
                    if (bsdld.getData().get(i).getPlayerId() == login.getData().get(j).getPlayerId()){
                        bsdld.getData().get(i).setPhone(login.getData().get(j).getPhone());
                        bsdld.getData().get(i).setType(login.getData().get(j).getPhone());
                        break;
                    }
                }
            }
            return bsdld;
        }
    }

    @PreAuthorize("hasAuthority('bsdld:get/playerDetailsInfo')")
    @ApiOperation(value = "玩家详细信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"), @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")})
    @GetMapping("/playerDetailsInfo")
    public PageResult<PlayerDetailsInfo> findPlayerDetailsInfo(@RequestParam Map<String, Object> params) {
        logger.info("玩家详细信息查询控制类");
//        if (!(params.containsKey("searchKey") && "playerId".equals(params.get("searchKey")))){
//            return null;
//        }
//        String playerId = params.get("searchValue").toString();
        String playerId = "1000";
        PlayerDetailsInfo bsdldInfo = bsdldService.findByPlayerId(playerId);
        bsdldInfo.toString();
        PlayerDetailsInfo loginInfo = playerBaseService.findByPlayerId(playerId);
        loginInfo.toString();
        if (bsdldInfo == null || loginInfo == null){
            return null;
        }
        loginInfo.setType(loginInfo.getPhone());
        loginInfo.setNickName(bsdldInfo.getNickName());
        loginInfo.setSex(bsdldInfo.getSex());
        List<PlayerBaseInfo> list = new ArrayList<>();
        list.add(loginInfo);
        PageInfo<PlayerDetailsInfo> pageInfo = new PageInfo(list);
        return PageResult.<PlayerDetailsInfo>builder().data(pageInfo.getList()).code(0).count(pageInfo.getTotal()).build();
    }

    @PreAuthorize("hasAuthority('bsdld:get/playerBagInfo')")
    @ApiOperation(value = "玩家背包信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"), @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")})
    @GetMapping("/playerBagInfo")
    public PageResult<PlayerBagInfo> playerBackpackInfo(@RequestParam Map<String, Object> params) {
        logger.info("玩家背包信息信息查询控制类");
        String playerId = "1000";
        PlayerBagInfo bsdldInfo = bsdldService.findBagByPlayerId(playerId);
        bsdldInfo.toString();
        if (bsdldInfo == null){
            return null;
        }
        List<PlayerBagInfo> list = new ArrayList<>();
        list.add(bsdldInfo);
        PageInfo<PlayerBagInfo> pageInfo = new PageInfo(list);
        return PageResult.<PlayerBagInfo>builder().data(pageInfo.getList()).code(0).count(pageInfo.getTotal()).build();
    }

}
